﻿using System;
using System.Windows.Forms;

namespace ZYCommon
{
    

    public class ClipboardHandler
    {
        public static bool CanGetData(string strFormat)
        {
            try
            {
                IDataObject dataObject = Clipboard.GetDataObject();
                return ((dataObject != null) && dataObject.GetDataPresent(strFormat));
            }
            catch (Exception exception)
            {
                ZYErrorReport.Instance.DebugPrint("CanGetData:试图获得" + strFormat + "格式的剪切板数据错误:" + exception.Message);
                return false;
            }
        }

        public static bool CanGetText()
        {
            try
            {
                IDataObject dataObject = Clipboard.GetDataObject();
                return ((dataObject != null) && dataObject.GetDataPresent(DataFormats.Text));
            }
            catch (Exception exception)
            {
                ZYErrorReport.Instance.DebugPrint("CanGetText:试图获得文本格式的剪切板数据错误:" + exception.Message);
                return false;
            }
        }

        public static object GetDataFromClipboard(string strFormat)
        {
            try
            {
                IDataObject dataObject = Clipboard.GetDataObject();
                if ((dataObject != null) && dataObject.GetDataPresent(strFormat))
                {
                    return dataObject.GetData(strFormat);
                }
            }
            catch (Exception exception)
            {
                ZYErrorReport.Instance.DebugPrint("GetDataFromClipboard:试图获得" + strFormat + "格式的剪切板数据错误:" + exception.Message);
            }
            return null;
        }

        public static string GetTextFromClipboard()
        {
            string str2="";
            try
            {
                IDataObject dataObject = Clipboard.GetDataObject();
                if (!dataObject.GetDataPresent(DataFormats.UnicodeText))
                {
                    return null;
                }
                string data = (string) dataObject.GetData(DataFormats.UnicodeText);
                str2 = data;
            }
            catch
            {
            }
            return str2;
        }

        public static bool SetTextToClipboard(string strText)
        {
            if ((strText != null) && (strText.Length > 0))
            {
                try
                {
                    DataObject data = new DataObject();
                    data.SetData(DataFormats.UnicodeText, true, strText);
                    Clipboard.SetDataObject(data, true);
                    return true;
                }
                catch (Exception exception)
                {
                    Console.WriteLine("Got exception while Copy text to clipboard : " + exception);
                }
            }
            return false;
        }
    }
}

